package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.demo.entity.DTO.UserDTO;
import com.example.demo.entity.Follow;
import com.example.demo.entity.User;
import com.example.demo.mapper.FollowMapper;
import com.example.demo.mapper.ForwardMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 关注表 服务实现类
 * </p>
 *
 * @author Mr.we
 * @since 2024-06-04
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public boolean addFollow(Follow f) {
        if(followMapper.insertFollow(f) == 1){
            return true;
        }
        return false;
    }

    /**
     * 根据用户ID查询所有关注
     * @param userId
     * @return
     */
    @Override
    public List<UserDTO> findFollowByUserId(Integer userId) {
        List<UserDTO> userDTOS = new ArrayList<>();
        List<Follow> allByUserId = followMapper.findFollowByUserId(userId);
        for (Follow follow : allByUserId) {
            UserDTO userDTO = new UserDTO();
            User user = userMapper.selectByUserId(follow.getFollowedUserId());
            BeanUtil.copyProperties(user,userDTO);
            userDTOS.add(userDTO);
        }
        return userDTOS;
    }

    @Override
    public List<UserDTO> findFollowedByUserId(Integer userId) {
        List<UserDTO> userDTOS = new ArrayList<>();
        List<Follow> allByUserId = followMapper.findFollowedByUserId(userId);
        for (Follow follow : allByUserId) {
            UserDTO userDTO = new UserDTO();
            User user = userMapper.selectByUserId(follow.getFollowUserId());
            BeanUtil.copyProperties(user,userDTO);
            userDTOS.add(userDTO);
        }
        return userDTOS;
    }

    @Override
    public boolean cancelFollow(Follow follow) {
        if(followMapper.cancelFollow(follow) == 1){
            return true;
        }
        return false;
    }

    @Override
    public boolean isHasFollow(Follow follow) {
        if(followMapper.findFollow(follow) !=null){
            return true;
        }
        return false;
    }
}
